﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.UserD
{
    public class UserDAL
    {
        private readonly StarsEASContext _context;

        public UserDAL(StarsEASContext context) { 
            _context = context;
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public ResultUtils GetAllUser() {
            StarsEASContext _context = new StarsEASContext();
            List<User> userList = _context.Users.ToList();
            if(userList==null)return new ResultUtils() { code=400,message="Error"};
            return new ResultUtils() { code = 200, message = userList };
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="data">用户信息</param>
        /// <returns></returns>
        public ResultUtils InsertUser(User data) {
            StarsEASContext _context = new StarsEASContext();
            data.Password = Utils.GenerateMD5(data.Password);
            data.Token = null;
            _context.Users.Add(data);
            _context.SaveChanges();
            return new ResultUtils() { code = 200, message = data };
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public ResultUtils UpdatePassword(string oldPwd, string newPwd, string userName) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where<User>(o => o.Password == Utils.GenerateMD5(oldPwd) && o.Username == userName).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"修改密码---原密码输入错误", true);
                return new ResultUtils() { code = 500, message = "原密码输入错误" };
            }
            user.Password = Utils.GenerateMD5(newPwd);
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"修改密码---{userName}的密码修改失败或与原密码相同", true);
                return new ResultUtils() { code = 500, message = "密码修改失败或与原密码相同" };
            }
            AppLog.WriteInfo($"修改密码---{userName}的密码修改成功",true);
            return new ResultUtils() {code=200,message= "succeed" };
        }


        //public ResultUtils ChangeThePasswordByPhone(string newPwd, string userName)
        //{

        //}

        public ResultUtils ForgotPassword(string idcard, string newPwd, string userName)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(p=>p.Username==userName).FirstOrDefault();
            if (user == null) {
                AppLog.WriteError($"忘记密码---用户名输入错误", true);
                return new ResultUtils() { code = 500, message = "用户名输入错误" };
            }
            if (user.PositionId == 1)
            {
                Model.Models.Student stu = _context.Students.Where(s => s.StuId == userName).FirstOrDefault();
                if (stu == null ) {
                    AppLog.WriteError($"忘记密码---未查询到该学生信息", true);
                    return new ResultUtils() { code = 500, message = "未查询到该学生信息" };
                }
                if (stu.StuCarid != idcard) {
                    AppLog.WriteError($"忘记密码---身份证号码输入错误", true);
                    return new ResultUtils() { code = 500, message = "身份证号码输入错误" };
                }
            }
            else {
                Worker worker = _context.Workers.Where(w => w.WorkerId == userName).FirstOrDefault();
                if (worker == null) {
                    AppLog.WriteError($"忘记密码---未查询到该教职工信息", true);
                    return new ResultUtils() { code = 500, message = "未查询到该教职工信息" };
                }
                if (worker.WorkerCarid != idcard) {
                    AppLog.WriteError($"忘记密码---身份证号码输入错误", true);
                    return new ResultUtils() { code = 500, message = "身份证号码输入错误" };
                }
            }
            user.Password = Utils.GenerateMD5(newPwd);
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"忘记密码---{userName}的密码修改失败或与原密码相同", true);
                return new ResultUtils() { code = 500, message = "密码修改失败或与原密码相同" };
            }
            AppLog.WriteInfo($"忘记密码---{userName}的密码修改成功", true);
            return new ResultUtils() { code = 200, message = "succeed" };
        }

    }
}
